Gnc.EmployeeListBox = function () {
    Gnc.EmployeeListBox.superclass.constructor.call(this);
};
mini.extend(Gnc.EmployeeListBox, mini.Box, {
    uiCls: "gnc-employeelistbox",
    listUrl:bootPATH+'Js/Common/Org/EmployeeListBoxView.htm',
    listbox:null,  
    _create: function () {
        Gnc.EmployeeListBox.superclass._create.call(this);
        var html = mini.getRemoteText(this.listUrl);
        this.setControls([html]);        

        this.fieldName = mini.getbyName("listbox-fieldName",this);
        this.isMaster=this.fieldName.getText().indexOf('主管')>=0?true:false;
        //人员的选择
        this.employeeSelect = mini.getbyName("employeeSelect",this);
        this.employeeTypes=mini.getbyName("employeeTypes",this); 

        //listbox        
        this.listbox = mini.getbyName("employee-listbox",this);
        this.listbox.on('itemclick',function(record){
            var name = record.item.name;
            var id = record.item.id;
            if(mini.isEmpty(this.employeeSelect.getValue()))
                this.employeeSelect.setNameValue(name,id);
            // this.fire('employeeSelected',record.item);
        },this);

        //添加按钮
        var btn = mini.getbyName("btn_addemployee",this);
        btn.on('click',function(){
            this.onAdd();
        },this);
        //编辑按钮
        var btn = mini.getbyName("btn_editemployee",this);
        btn.on('click',function(){
            this.onEdit();
        },this);
        //删除按钮
        btn = mini.getbyName("btn_removeemployee",this);
        btn.on('click',function(){
        	this.onDelete();
        },this);
        // this.employeeSelect.on('employeeAutoSelected',function(obj){
        //     this.account=obj.loginAccount;      
        // },this);
    },
	//添加
    onAdd:function() {
        var me = this;
        var name = me.employeeSelect.getText();
        var value = me.employeeSelect.getValue();
        if(mini.isEmpty(value)) {
            return;
        }
        var list=me.listbox.getData();
        for(var i=0;i<list.length;i++) {
            var Id=list[i].id;
            if(Id.indexOf(value)!= -1) {
                mini.alert('该账号已存在，不能重复添加', '警告');
                me.employeeSelect.setNameValue('','');
                return;
            }
        }
        this.addItem(name,value);
        me.employeeSelect.setNameValue('','');        
    },
    //执行添加
    addItem:function(name,value){
        var me = this;        
        me.listbox.addItem({id:value, name:name });
        // this.modifyValue();
    },
    //编辑
    onEdit:function() {
        var me=this;
        var selecteds = me.listbox.getSelecteds();
        if(selecteds.length==0){
             mini.alert('请选择需要编辑的员工账号！', '警告');
             return ;
        }
        var value = me.employeeSelect.getValue();
        if(mini.isEmpty(value)) {
            return;
        }
        if(selecteds.length>1){
             mini.alert('只能选择一条需要编辑的员工账号！', '警告');
             return ;
        }
        if(selecteds[0].id!=value){
            var list=me.listbox.getData();
            for(var i=0;i<list.length;i++){
                var Id=list[i].id;
                if(Id.indexOf(value)!= -1){
                    mini.alert('该账号已存在，不能重复添加', '警告');
                    me.employeeSelect.setNameValue('','');
                    return;
                }
            }
        }
        if(me.onDelete()){
            me.onAdd();
        }
    },
    //执行删除
    onDelete:function(){
    	var me = this;
        var selecteds = me.listbox.getSelecteds();
        if(mini.isEmpty(selecteds)){
             mini.alert('请选择负责人选项！', '警告');
             return false;
        }
        if(selecteds.length==0){
             mini.alert('请选择需要清除的员工账号！', '警告');
             return ;
        }
        me.listbox.removeItems(selecteds);
        // this.modifyValue();
        this.employeeSelect.setNameValue('','');
        return true;
    },
    setDefaultValue:function(attributes){
        if(!attributes) return;
        var me = this;
        var params = attributes.split(';');
        jQuery(params).each(function(index,param){
            try{
                var field = param.split(':')[0].trim();
                var value = param.split(':')[1].trim();
                if(field=='fieldName')
                    me.fieldName.setText(value+":");
            }catch(ex){}
        });
    },
    // modifyValue:function(){
    //     var me = this;
    //     var ids = [];
    //     jQuery(me.listbox.getData()).each(function(index,record){
    //         ids.push(record.id);
    //     });
    //     me.value = ids.toString();
    // },
    setFieldName:function(fName){
        this.isMaster=fName.indexOf('主管')>=0?true:false;
    },
    hideField: function () {
        this.fieldName.hide();
    },
    getListData:function(){
       return this.listbox.getData();
    },
    getItemSet:function(){
        var data = {id:'',name:''};
        var items = this.listbox.getData();
        if(items.length > 0){
            for(var i=0;i<items.length;i++){
                var d = items[i];
                data.id += ',' + d.id;
                data.name += ',' + d.name;
            };
            data.id = data.id.substring(1);
            data.name = data.name.substring(1);
        }
       return data;
    },
    getValue:function(){
        var ids = [];
        var items = this.listbox.getData();
        jQuery(items).each(function(index,record){
            ids.push(record.id);
        });
        return ids.toString();
    }
});
mini.regClass(Gnc.EmployeeListBox, "Gnc.EmployeeListBox");